package com.dingding.action;

import java.io.InputStream;

import org.apache.log4j.Logger;

import com.dingding.bean.api.wechat.WechatPayResult;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.common.core.util.XMLParse;

/**
 * @ClassName: WechatPayNotifyAction
 * @Description: 微信支付相应action，用来接收微信传过来的用户支付结果
 * @author fengshuonan
 * @date 2016年3月9日 下午8:43:58
 * @version 叮叮搭车2.0
 */
@SuppressWarnings("all")
public class WechatPayNotifyAction extends BaseAction {

	private Logger log = Logger.getLogger(this.getClass());
	
	/**
	 * @Title: getPayResult
	 * @Description:接受微信支付结果
	 * @author fengshuonan
	 * @return String
	 * @throws
	 */
	public String getPayResult(){
		
		InputStream inputStream = super.getRequestInputStream();
		
		//读取接收到微信服务器传来的xml信息
		String requestXml = ToolUtil.readStreamToText(inputStream);
		
		log.info("微信传来的支付结果如下:" + requestXml);
		
		//把接受到微信服务器传来的xml信息转化为实体
		WechatPayResult wechatPayResult = XMLParse.toObject(requestXml, WechatPayResult.class);
		
		//根据支付结果进行相关业务 
		boolean flag = super.userOrderService.verifyWechatPayResult(wechatPayResult);
		
		//当服务器成功接收到通知并校验成功，返回给微信ok
		if(flag){
			responseOkToWechat();
		}else{
			responseFailToWechat();
		}
		
		return NONE;
	}
	
	//响应给微信接并处理收信息成功
	private void responseOkToWechat(){
		String str = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
		super.renderText(str);
	}
	
	//响应给微信接并处理收信息失败
	private void responseFailToWechat(){
		String str = "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[FAIL]]></return_msg></xml>";
		super.renderText(str);
	}
	
}
